//
//  YewenCollegeViewModel.m
//  jobClient
//
//  Created by 小细菌 on 2021/3/9.
//  Copyright © 2021 YL1001. All rights reserved.
//

#import "YewenCollegeViewModel.h"
#import "ELTrainingClassQuestionModel.h"
#import "ELPictureVerifyViewController.h"
#import "HPSZCheckContext.h"
#import "QuestionCheckCtl.h"
#define Upload_Sec 60    //上报周期
#define HookPop_Sec 20*60  //挂机15分钟退出

@interface YewenCollegeViewModel()
@property (nonatomic, assign) NSInteger playDuration;
@property (strong, nonatomic) HPSZCheck *supervision;

@property (nonatomic, strong) RACSubject *detailSubject; //详情
@property (nonatomic, strong) RACSubject *synchronousSubject; //60s上报
@property (nonatomic, strong) RACSubject *rulsSubject; //打卡规则
@property (nonatomic, strong) RACSubject *progressSubject; //进度
@property (nonatomic, strong) RACSubject *signSubject;//签到
@property (nonatomic, strong) RACSubject *checkResultSubject;//打卡成功/失败
@property (nonatomic, strong) RACDisposable *handler ;

@property (nonatomic, assign) NSInteger serversTimestamp;     // 系统时间
@property (nonatomic, strong) NSString *logId; // 上报进度的记录id，用于标记同一次学习所产生的进度记录
@property (nonatomic, assign) NSInteger lastReportTime; //上一次60s上报的时间

@property (nonatomic, strong) NSMutableArray<ELTrainingClassQuestionModel*> *regularCheckAry;
@property (nonatomic, strong) ELTrainingClassQuestionModel *rule;

@property (nonatomic, strong) QuestionCheckCtl *questionCtrl;
@property (atomic) BOOL doingCheck;
@property (atomic) BOOL strongExit;

@end



@implementation YewenCollegeViewModel

- (id)initWithArticleId:(NSString*)articleId groupId:(NSString*)groupId{
    YewenCollegeViewModel* vm = YewenCollegeViewModel.new;
    vm.article_id = articleId;
    vm.group_id = groupId;
    return vm;
}
- (void)gx_init{
    self.serversTimestamp = [NSDate date].timeIntervalSince1970;
    self.detailSubject = [RACSubject subject];
    self.synchronousSubject = [RACSubject subject];
    self.rulsSubject = [RACSubject subject];
    self.progressSubject = [RACSubject subject];
    self.signSubject = [RACSubject subject];
    self.checkResultSubject = [RACSubject subject];
    self.regularCheckAry = [NSMutableArray new];
  
    @weakify(self)
    [self.detailSubject subscribeNext:^(YewenMediaDetailModel* yewen){
        @strongify(self);
        if(self.yewenModel.media_info.media_type.intValue == 2 && !self.isComplete){
            [self requestYewenPlayRecord];
            [self requestYewenRegularCheck];
            [self YewenshouldSignCheck];
        }else{
            [self.progressSubject sendNext:nil];
            [self.progressSubject sendCompleted];
            [self.rulsSubject sendNext:nil];
            [self.rulsSubject sendCompleted];
            [self.signSubject sendNext:nil];
            [self.signSubject sendCompleted];
        }
    }];
    
    self.loadFinishedSubject = [RACSignal combineLatest:@[self.detailSubject,self.progressSubject  , self.rulsSubject ,self.signSubject ] reduce:^(YewenMediaDetailModel* detail, NSNumber* needCheck  , NSMutableArray* rulesArray , NSString* hasSign){
        @strongify(self);
        if(self.yewenModel.media_info.media_type.intValue == 2){
            if([needCheck boolValue]){
                if ([hasSign intValue] == 0) { //未签到   把签到作为第0秒打卡
                    ELTrainingClassQuestionModel* check = ELTrainingClassQuestionModel.new;
                    check.time = @"2";
                    check.select_type = @"1";
                    [rulesArray insertObject:check atIndex:0];
                }
                [self addListener];
                //xxxxxxxx开源库修改xxxxxxxxx//
                self.player.defaultEdgeControlLayer.showsMoreItem = APPCONFIG.training.ablePlaySpeedControl;
                self.player.defaultEdgeControlLayer.bottomProgressIndicatorEnable = APPCONFIG.training.ableDrag;
            }else{
                [self authComplete];
                self.player.defaultEdgeControlLayer.bottomProgressIndicatorEnable = YES;
                self.regularCheckAry = nil;
            }
        }else{
            [self authComplete];
            self.regularCheckAry = nil;
        }
        return detail;
    }];
    
    
    [[[self.synchronousSubject throttle:1] filter:^BOOL(id x){
        @strongify(self);
        return [x integerValue] - self.playLastProgress > 10;
    }] subscribeNext:^(id x){
        @strongify(self);
        [self synchronousLearning];
    }];
    
    //打卡结果
    [[self.checkResultSubject deliverOnMainThread] subscribeNext:^(id x){
        @strongify(self);
//        NSLog(@"%@",self.regularCheckAry);
        if(x){
            [ELShowAlertView showAlertWithTitle:@"提示" content:x letBtnTitle:@"退出" rightBtnTitle:@"重试" leftBlock:^(id x){
                self.regularCheckAry = nil;
                self.videoUrl = nil;
                self.doingCheck = NO;
                [self.delegate performSelector:@selector(popViewController)];
            } rightBlock:^(id x){
                [self beginCheck];
            }];
        }else{
            if (self.regularCheckAry.count > 0) {
                [self.regularCheckAry removeObjectAtIndex:0];
            }
            self.doingCheck = NO;
            [self.continueSubject sendNext:nil];
            NSLog(@"信号量 ---解锁");
        }
    }];
}


- (void)gx_viewDidLoad{
    [self beginRequest];
}
 
- (void)gx_viewWillAppear{

}

- (void)gx_viewWillDisappear{
    if(!_isComplete && self.currentTime>0){
        [self synchronousLearning];
    }
}


- (void)dealloc{
    NSLog(@"%@ -- dealloc",self);
    [self authComplete];
}

- (void)beginRequest{
    
    [self requestYewenAudioDetail];

}


- (void)addListener{
    @weakify(self)
    self.handler = [[[[RACObserve(self,currentTime) deliverOnMainThread] distinctUntilChanged] ignore:@(0)] subscribeNext:^(id x) {
        @strongify(self);
        NSInteger time = [x integerValue];
        NSLog(@"当前--%ld 下一个点--%@ -----%@" , time , self.rule.time,self.rule);
        if(self.isComplete)
            return;
        
        if(time >= self.rule.time.integerValue && self.rule){
            [self.pauseSubject sendNext:nil];
        }else if(time % Upload_Sec == 0 ){
            [self.synchronousSubject sendNext:@(time)];
        }else{
            NSLog(@"-----有问题----当前--%ld 下一个点--%@ -----%@", time , self.rule.time,self.rule);
        }
    }];
    
    [self.onHookListener subscribeNext:^(id x){
        @strongify(self);
        NSLog(@"-----挂机太久----onHookListener");
        self.strongExit = YES;
        if(!self.isComplete){
            self.videoUrl = nil;
            [self authComplete];
            [self popViewDuetoHook];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [BaseUIViewController showAlertView:@"提示" msg:@"挂机太久，请重新学习" btnTitle:@"确定"];
            });
        }
    }];
    
    
    [[self.onPauseListener throttle:1] subscribeNext:^(id x){
        @strongify(self);
        NSLog(@"----暂停---%lds-onPauseListener",self.currentTime);
        if(self.isComplete)
            return;
        [self.synchronousSubject sendNext:@(self.currentTime)];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if([[Manager getCurrentVC] isKindOfClass:NSClassFromString(@"YewenCollegeCtrl")])
                [self performSelector:@selector(popViewDuetoHook) withObject:nil afterDelay:HookPop_Sec];
        });
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self actionCheck];
        });
    }];
    
    [[self.onPlayListener deliverOnMainThread] subscribeNext:^(id x){
        @strongify(self);
        NSLog(@"----播放---%ld-onPlayListener",self.currentTime);
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(popViewDuetoHook) object:nil];
    }];
    
    [[self.onFinishListener deliverOnMainThread] subscribeNext:^(id x){
        @strongify(self);
        NSLog(@"----视频播完了---%ld-onFinishListener",self.currentTime);
//        [self.synchronousSubject sendNext:@(self.player.duration-1)];
        self.playNext = YES;
        [self synchronousLearning];
        [self authComplete];
    }];
    
}


-(void)actionCheck{
    NSLog(@"信号量 上锁");
    
    if (self.doingCheck) {
        return;
    }
    self.doingCheck = YES;
    
    if (self.currentTime < self.rule.time.intValue && self.rule) {
        NSLog(@"信号量 ---解锁 --- 还没到打卡点呢");
        self.doingCheck = NO;
        return;
    }
    
    if(!self.rule){
        NSLog(@"信号量 ---解锁 --- 不打卡了 打光了");
        self.doingCheck = NO;
        return;
    }
    
    NSLog(@"%@开始打卡-%ld--%@--%@" , [MyCommon getSecondMinuteStrWithTimer:self.currentTime] , self.currentTime , self.rule.time,self.rule.mj_JSONObject);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self beginCheck];
    });
    
}


//挂机导致pop页面
- (void)popViewDuetoHook{
    NSLog(@"popViewDuetoHook 触发了");
    self.doingCheck = NO;
    
    [self.questionCtrl remove];
    
    [self.delegate performSelector:@selector(popViewController)];
}
 
- (void)authComplete{
    self.isComplete = YES;
    self.regularCheckAry = nil;
    self.playLastProgress = 0;
    [self.onPauseListener sendCompleted];
    [self.onPlayListener sendCompleted];
    [self.pauseSubject sendCompleted];
    [self.onHookListener sendCompleted];
    [self.synchronousSubject sendCompleted];
    [self.handler dispose];
}
- (void)beginCheck{
    
    if(self.isComplete) {
        self.doingCheck = NO;
        return;
    }
    
    if(![[Manager getCurrentVC] isKindOfClass:NSClassFromString(@"YewenCollegeCtrl")]){
        self.doingCheck = NO;
        NSLog(@"当前页面不是播放页,就不弹人脸了 信号量解锁");
        return;
    }
    
    if(self.rule.select_type.intValue == 1 )
        [self faceCheck];
    else if(self.rule.select_type.intValue == 3 )
        [self questionCheck];
    else if(self.rule.select_type.intValue == 2 )
        [self cardCheck];
    else{
        GXAlert(@"提示", @"打卡异常", @"知道了", nil, ^(NSInteger i){
            [self popViewDuetoHook];
        });
    }
}
#pragma mark ------  打卡 --------
//问题打卡
- (void)questionCheck{
    _questionCtrl = [[UIStoryboard storyboardWithName:SbEnterpriseUniversity bundle:[NSBundle mainBundle]]  instantiateViewControllerWithIdentifier:@"QuestionCheckCtl"] ;
    _questionCtrl.model = self.rule.question;
    [_questionCtrl show];
    @weakify(self);
    _questionCtrl.actionBlock = ^(BOOL finished){
        @strongify(self);
        if (finished) {
            [self commitQuestion];
        }else{
            [self.checkResultSubject sendNext:@"打卡失败"];
        }
    };
}
//滑块打卡
- (void)cardCheck{
    WS(weakSelf)
    NSLog(@"开始人脸打卡 %@",self.videoTitle);
    self.supervision = [HPSZCheckContext initContext:HPSZCheckTypeSlipSheet];
    [_supervision checkBegin:^(HPSZCheckResult * _Nonnull result) {
        if (result.code == HPSZ_SUCCESS ) {
            [weakSelf commitImageUrl:nil  orderNo:nil];
        }else {
            [weakSelf.checkResultSubject sendNext:@"打卡失败"];
        }
    }];
}
//人脸打卡
- (void)faceCheck{

    WS(weakSelf)
    NSLog(@"开始人脸打卡 %@",self.videoTitle);
    self.supervision = [HPSZCheckContext initContext:self.getFaceType];
    [_supervision checkBegin:^(HPSZCheckResult * _Nonnull result) {
        if (result.code == HPSZ_SUCCESS && result.images.count > 0) {
            UIImage* image = result.images[0];
            [weakSelf.supervision uploadImages:@[image] type:HPSZCheckUploadCert onFinish:^(HPSZCheckResult * _Nonnull result) {
                if (result.code == HPSZ_SUCCESS && result.imagePaths.count > 0) {
                    [weakSelf commitImageUrl:result.imagePaths[0]  orderNo:nil];
                }else {
                    [weakSelf.checkResultSubject sendNext:@"打卡失败"];
                }
            }];
        }else {
            NSString *msg;
            if (result.code == HPSZ_FAIL_CAMERA) {
                msg = @"相机权限没打开，请到App设置中允许业问访问相机权限";
            }else {
                msg = @"生物识别失败";
            }
            [weakSelf.checkResultSubject sendNext:msg];
        }
    }];
    
}

#pragma mark ------ getter setter --------
- (ELTrainingClassQuestionModel *)rule{
    @synchronized (self) {
        id object = self.regularCheckAry.count > 0 ? self.regularCheckAry[0] : nil;
        if(object == nil){
//            [self authComplete];
//            [self.handler dispose];
            self.regularCheckAry = nil;
//            NSLog(@"没有打卡点了");
        }
        return object;
    }
}

 
#pragma mark ------ 网络请求 -------

// 获取最近的播放的进度
- (void)requestYewenPlayRecord{
    
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getLastPlayRecord:@"3" forvalue:_article_id ];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"播放进度---%@",result);
        if ([result[@"code"] intValue] == 200) {
            NSDictionary *info = [result objectForKey:@"data"];
            // 是否播放完，已播完的允许进度拖动
            self.playLastProgress = 0;
            NSNumber *is_complete = [info objectForKey:@"is_complete"];
            if (is_complete.intValue == 1) {
                self.isComplete = YES;
                self.playLastProgress = 0;
            }else {
                NSNumber *time = [info objectForKey:@"time"];
                self.playLastProgress  = time.integerValue;
            }           
            [self.progressSubject sendNext: @(1-self.isComplete)];
        }else{
            [self.progressSubject sendError:result[@"status_desc"]];
        }
        [self.progressSubject sendCompleted];
    } failure:^(NSString * _Nullable error) {
        NSLog(@"获取播放进度网络异常");
        [self.progressSubject sendError:@"获取播放进度网络异常"];
        [self.progressSubject sendCompleted];
    }];
}


//视频详情------每次播放前都会请求这个接口
- (void)requestYewenAudioDetail{
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getWorksInfo:_article_id projectId:APPCONFIG.training.projectId group_id:self.group_id];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"视频详情---%@",result);
        if ([result[@"code"] intValue] == 200) {
            APPCONFIG.training.projectId = result[@"data"][@"project_id"]; //我也不知道为什么要加这行代码
            YewenMediaDetailModel* model = [YewenMediaDetailModel yy_modelWithDictionary:result[@"data"]];
            _yewenModel = model;

            [self.detailSubject sendNext:model];
        }else{
            [self.detailSubject sendError:result[@"status_desc"]];
        }
        [self.detailSubject sendCompleted];
    } failure:^(NSString * _Nullable error) {
        [self.detailSubject sendError:@"视频详情网络异常"];
        [self.detailSubject sendCompleted];
    }];
}

//签到
- (void)YewenshouldSignCheck{
    if(APPCONFIG.training.checkSignType==0){
        NSLog(@"不需要请求签到");
        [self.signSubject sendNext:@(1)];
        [self.signSubject sendCompleted];
        return;
    }
    
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model isSignIn:_article_id];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"签到 = %@",result);
        if([[result objectForKey:@"code"] intValue] == 200){
            NSString *isDidSign = [result objectForKey:@"data"];
//            if (isDidSign.boolValue)
//                return; // 已签到
            [self.signSubject sendNext:isDidSign];
        }else {
            [self.signSubject sendError:result[@"status_desc"]];
        }
        [self.signSubject sendCompleted];
    } failure:^(NSString * _Nullable error) {
        [self.signSubject sendError:@"签到网络异常"];
        [self.signSubject sendCompleted];
    }];
}

//打卡规则
-(void)requestYewenRegularCheck{
    if(APPCONFIG.training.checkRegularType==0){
        NSLog(@"不需要请求打卡规则");
        self.regularCheckAry = NSMutableArray.new;
        [self.rulsSubject sendNext:self.regularCheckAry];
        [self.rulsSubject sendCompleted];
        return;
    }
    
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getPlayRecordAuth:@"3" forvalue:_article_id];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        __weak typeof(self) weakSelf = self;
        NSLog(@"打卡规则 = %@",result);
        if([[result objectForKey:@"code"] intValue] == 200){
            NSDictionary *data = result[@"data"];
            NSArray *resultArr = data[@"auth_time"];
            // 服务端时间作为开始学习时间
            NSString *timestamp = [NSString stringWithFormat:@"%@",data[@"timestamp"]];
            if (timestamp != nil && timestamp.length == 10) {
                weakSelf.serversTimestamp = timestamp.integerValue;
            }
            if (ArrayIsNotEmpty(resultArr)) {
                weakSelf.regularCheckAry = NSMutableArray.new;
                for (NSDictionary * dic in resultArr) {
                    ELTrainingClassQuestionModel * model = [ELTrainingClassQuestionModel yy_modelWithJSON:dic];
                        if (!StringIsEmpty(model.time)) {
                            [weakSelf.regularCheckAry addObject:model];
                        }
                }
            }
            [self.rulsSubject sendNext:self.regularCheckAry];
        }else {
            [self.rulsSubject sendError:result[@"status_desc"]];
        }
        [self.rulsSubject sendCompleted];
    } failure:^(NSString * _Nullable error) {
        [self.rulsSubject sendError:@"定时抽查网络异常"];
        [self.rulsSubject sendCompleted];
    }];
}

//60s上报
- (void)synchronousLearning {
    if(self.isComplete || self.strongExit) return;
    
    @synchronized (self) {
        
//        if(self.currentTime - self.lastReportTime <= 1  && self.currentTime!=self.player.duration){
//            NSLog(@"当前%ld 上一次上报%ld 相差%ld 相隔太短了了不上报",self.currentTime,self.lastReportTime,self.currentTime-self.lastReportTime);
//            return;
//        }
        
        NSInteger playEndTime = [NSDate date].timeIntervalSince1970;
        NSInteger playStartTime = self.serversTimestamp;
        if (playEndTime - playStartTime < 5) {
            NSLog(@"----播放时长太短，少于5秒，不上传进度");
            return;
        }
        
        NSInteger timeTotal = self.player.duration;
        if (timeTotal <= 0) {
            NSLog(@"----视频时长为0有可能是加载失败，所以不同步学时");
            return;
        }
        
        [NSThread sleepForTimeInterval:0.1];
        NSInteger timeNow = self.player.currentTime;
        NSString *currentTime = [NSString stringWithFormat:@"%ld",timeNow];
        NSString *duration = [NSString stringWithFormat:@"%ld",timeTotal];
        NSMutableDictionary *hnjgParam = [NSMutableDictionary dictionary];
        if (APPCONFIG.training.project_source.intValue == 60) {//河南监管
            [hnjgParam setObject:self.userAccessToken forKey:@"userAccessToken"];
            [hnjgParam setObject:self.traceId forKey:@"traceId"];
            [hnjgParam setObject:self.isComplete?duration:currentTime forKey:@"playProcess"];
        }
    //    self.playLastProgress = timeNow;
        NSLog(@"----60s 上报--------%ld",(long)timeNow);
        self.lastReportTime = self.currentTime;
        __weak typeof(self) weakSelf = self;
        ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
        [model reportStudyProgress:_article_id
                         stat_hour:self.isComplete?duration:currentTime
                        total_hour:duration
                     last_playtime:[NSString stringWithFormat:@"%ld",timeNow]
                         course_id:_group_id
                       works_title:@"0"//_yewenModel.media_info.title
                    stat_begintime:[NSString stringWithFormat:@"%ld",playStartTime]
                      stat_endtime:[NSString stringWithFormat:@"%ld",playEndTime]
                           stat_id:_logId
                         stat_type:self.yewenModel.media_info.type
                         hnjgParam:hnjgParam];
         
         [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
            NSDictionary *resultdic = result;
            if (DictIsNotEmpty(resultdic)) {
                NSLog(@"60s上报成功%@",result);
                if (((NSString *)resultdic[@"code"]).intValue == 200) {
                    weakSelf.logId = [NSString stringWithFormat:@"%@",[resultdic valueForKeyPath:@"data.log_id"]];
                    NSString *percent = [resultdic valueForKeyPath:@"data.works_percent"];
                    if (!percent || ![percent isKindOfClass:[NSString class]] || percent.length == 0) return;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"yewenCollegeRefreshProgress" object:nil userInfo:@{@"progress": percent,@"article_id":_article_id}];
                        
                }else if (((NSString *)resultdic[@"code"]).intValue == 1050){
                    //
                    [self.pauseSubject sendNext:nil];
                    self.videoUrl = nil;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(350 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
                        [weakSelf.delegate alertWithTitle:@"温馨提示"
                                                  message:resultdic[@"status_desc"]
                                        cancelButtonTitle:@"知道了"
                                        otherButtonTitles:nil handler:^(UIAlertAction * _Nonnull action) {
                            [self.delegate performSelector:@selector(popViewController)];
                        }];
                    });
                }else if (((NSString *)resultdic[@"code"]).intValue == 1103){
                    //
                    [self.pauseSubject sendNext:nil];
                    self.videoUrl = nil;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(350 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
                        [weakSelf.delegate alertWithTitle:@"温馨提示"
                                                  message:resultdic[@"status_desc"]
                                        cancelButtonTitle:@"知道了"
                                        otherButtonTitles:nil handler:^(UIAlertAction * _Nonnull action) {
                            [self.delegate performSelector:@selector(popViewController)];
                        }];
                    });
                }
            }
             if (weakSelf.playNext) {
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"yewenCollegePlayEnd" object:nil];
                 weakSelf.playNext = NO;
             }
        } failure:^(NSString * _Nullable error) {
            NSLog(@"60s上报error = %@",error);
        }];
    }
}

//提交打卡
- (void)commitImageUrl:(NSString *)imgUrl orderNo:(NSString *)orderNo {
        
    NSString* type = @"auth";
    BOOL isFaceCheck = imgUrl!=nil;
    type = isFaceCheck?@"auth":@"supervise_sign"; //question_sign
    NSLog(@"上报打卡");
    
    
    NSMutableDictionary * conditionarr = [[NSMutableDictionary alloc] init];
    [conditionarr setObject: orderNo forKey:@"order_sn"];
    [conditionarr setObject: imgUrl forKey:@"pic"];
    [conditionarr setValue:type forKey:@"type"];
    [conditionarr setValue:APPCONFIG.training.taskId forKey:@"task_id"];
    [conditionarr setObject:_group_id forKey:@"course_id"];
    [conditionarr setObject:APPCONFIG.training.projectId forKey:@"project_id"];
    [conditionarr setObject:self.rule.is_process_face_auth forKey:@"is_process_face_auth"];
    HudShow;
    [GXNetworking requestWithParam:@{@"forvalue":_article_id} conditionParam:conditionarr op:@"yewen_college_busi" funtion:@"reportSignRecord" succeed:^(NSDictionary *dic){
        HudHide;
        [self.checkResultSubject sendNext:nil];
        toast(@"打卡成功");
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
//            [self synchronousLearning];
//        });
    } failed:^(int code ,NSString* msg){
        HudHide;
        [self.checkResultSubject sendNext:msg];
    }];
}


//提交答题打卡
- (void)commitQuestion {
    
    NSLog(@"答题打卡上报打卡");
    NSMutableDictionary * conditionarr = [[NSMutableDictionary alloc] init];
    [conditionarr setValue:@"question_sign" forKey:@"type"];
    [conditionarr setValue:APPCONFIG.training.taskId forKey:@"task_id"];
    [conditionarr setObject:_group_id forKey:@"course_id"];
    [conditionarr setObject:APPCONFIG.training.projectId forKey:@"project_id"];
    [conditionarr setObject:self.rule.question.id_ forKey:@"question_id"];
    
    NSMutableString* s = NSMutableString.new;
    for (int i = 0 ; i < self.rule.question.option_select.count; i++) {
        if(i != 0)
           [s appendString:@","];
        [s appendFormat:@"%@",self.rule.question.option_select[i]];
    }
    [conditionarr setObject:s forKey:@"desc"];
    
    HudShow;
    [GXNetworking requestWithParam:@{@"forvalue":_article_id} conditionParam:conditionarr op:@"yewen_college_busi" funtion:@"reportSignRecord" succeed:^(NSDictionary *dic){
        HudHide;
        [self.checkResultSubject sendNext:nil];
        toast(@"打卡成功");
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
//            [self synchronousLearning];
//        });
    } failed:^(int code ,NSString* msg){
        HudHide;
        [self.checkResultSubject sendNext:msg];
    }];
}
@end
